package start.doubudouyin.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import start.doubudouyin.entity.DateNum;
import start.doubudouyin.entity.join.VideoPlay;
import start.doubudouyin.entity.Videoinfo;
import start.doubudouyin.mapper.IdgenerateMapper;
import start.doubudouyin.service.VideoinfoService;
import start.doubudouyin.mapper.VideoinfoMapper;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author mmmm
 * @description 针对表【videoinfo(视频信息表)】的数据库操作Service实现
 * @createDate 2025-06-09 14:26:27
 */
@Service
public class VideoinfoServiceImpl extends ServiceImpl<VideoinfoMapper, Videoinfo>
        implements VideoinfoService {

    @Resource
    private VideoinfoMapper videoinfoMapper;
    @Resource
    private IdgenerateMapper idgenerateMapper;

    public Integer Videoupload(Videoinfo videoinfo) {
        return videoinfoMapper.insert(videoinfo);
    }

    public Integer Videoinfoupload(String title, String description, String videoLabel, String videoId) {
        return videoinfoMapper.Videoinfoupload(title, description, videoLabel, videoId);
    }

    public Integer GetLikesCount(String userId) {
        return videoinfoMapper.GetLikesCount(userId);
    }

    @Override
    public List<Integer> getVideoLabelsData() {
        List<String> list = videoinfoMapper.getVideoLabelsData();
        List<Integer> videoLabelData = new ArrayList<>();
        // 确定需要的结果列表长度
        int maxIndex = 0;
        for (String item : list) {
            if (!item.isEmpty()) {
                String[] numbers = item.split(",");
                for (String num : numbers) {
                    int index = Integer.parseInt(num);
                    maxIndex = Math.max(maxIndex, index);
                }
            }
        }
        // 确保结果列表足够长
        while (videoLabelData.size() <= maxIndex) {
            videoLabelData.add(0);
        }

        // 统计数据
        for (int i = 0; i < list.size(); i++) {
            String item = list.get(i);
            if (item.isEmpty()) {
                // 如果源列表中对应位置没有数据，则在结果列表对应位置+1
                //videoLabelData.set(0, videoLabelData.get(0) + 1);
                continue;
            } else {
                // 如果源列表中有数据，则解析数据并在对应位置+1
                String[] numbers = item.split(",");
                for (String num : numbers) {
                    int index = Integer.parseInt(num) - 1;
                    videoLabelData.set(index, videoLabelData.get(index) + 1);
                }
            }
        }
        return videoLabelData;
    }

    @Override
    public DateNum getVideoNumRange30() {
        List<Map<String, Object>> videoNumRange30 = videoinfoMapper.getVideoNumRange30();
        List<Object> date = videoNumRange30.stream().map(dateMap -> dateMap.get("date")).toList();
        List<Object> num = videoNumRange30.stream().map(numMap -> numMap.get("num")).toList();
        return new DateNum(date, num);
    }

    public List<VideoPlay> FindAll(Integer videoStatus){
        return videoinfoMapper.FindAll(videoStatus);
    }

    public Videoinfo GetVideoInfoById(String videoId) {
        return videoinfoMapper.GetVideoInfoById(videoId);
    }

    public Integer UpdateVideoStatus(Integer videoStatus, String videoId) {
        return videoinfoMapper.UpdateVideoStatus(videoStatus, videoId);
    }

    public Integer deleteVideoById(String videoId) {
        return videoinfoMapper.deleteVideoById(videoId);
    }

    public Integer updateVideoPassTime(String videoId) {
        return videoinfoMapper.updateVideoPassTime(videoId);
    }


}




